package cn.iocoder.yudao.module.system.controller.admin.dict;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.dict.vo.data.DictDataExtListReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dict.vo.data.DictDataExtRespVO;
import cn.iocoder.yudao.module.system.controller.admin.dict.vo.data.DictDataExtSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dict.vo.data.DictDataExtSimpleRespVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dict.DictDataExtDO;
import cn.iocoder.yudao.module.system.service.dict.DictDataExtService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 级联下拉")
@RestController
@RequestMapping("/system/dict-data-ext")
@Validated
public class DictDataExtController {

    @Resource
    private DictDataExtService dictDataExtService;


    @PostMapping("create")
    @Operation(summary = "创建字典树")
    @PreAuthorize("@ss.hasPermission('system:dictdataext:create')")
    public CommonResult<Long> createDept(@Valid @RequestBody DictDataExtSaveReqVO createReqVO) {
        Long deptId = dictDataExtService.createDictDataExt(createReqVO);
        return success(deptId);
    }

    @PutMapping("update")
    @Operation(summary = "更新字典树")
    @PreAuthorize("@ss.hasPermission('system:dictdataext:update')")
    public CommonResult<Boolean> updateDept(@Valid @RequestBody DictDataExtSaveReqVO updateReqVO) {
        dictDataExtService.updateDictDataExt(updateReqVO);
        return success(true);
    }

    @DeleteMapping("delete")
    @Operation(summary = "删除字典树")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:dictdataext:delete')")
    public CommonResult<Boolean> deleteDept(@RequestParam("id") Long id) {
        dictDataExtService.deleteDictDataExt(id);
        return success(true);
    }

    @GetMapping("/list")
    @Operation(summary = "获取字典树列表")
    @PreAuthorize("@ss.hasPermission('system:dictdataext:query')")
    public CommonResult<List<DictDataExtRespVO>> getDeptList(DictDataExtListReqVO reqVO) {
        List<DictDataExtDO> list = dictDataExtService.getDictDataExtList(reqVO);
        return success(BeanUtils.toBean(list, DictDataExtRespVO.class));
    }

    @GetMapping(value = {"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取字典树精简信息列表", description = "只包含被开启的部门，主要用于前端的下拉选项")
    public CommonResult<List<DictDataExtSimpleRespVO>> getSimpleDeptList() {
        List<DictDataExtDO> list = dictDataExtService.getDictDataExtList(
                new DictDataExtListReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus()));
        return success(BeanUtils.toBean(list, DictDataExtSimpleRespVO.class));
    }

    @GetMapping("/get")
    @Operation(summary = "获得字典树信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:dictdataext:query')")
    public CommonResult<DictDataExtRespVO> getDept(@RequestParam("id") Long id) {
        DictDataExtDO dept = dictDataExtService.getDictDataExt(id);
        DictDataExtRespVO resp = BeanUtils.toBean(dept, DictDataExtRespVO.class);
        resp.setParent(dictDataExtService.getDictDataExt(dept.getParentId()));
        return success(resp);
    }


    @GetMapping("/tree")
    @Operation(summary = "获取字典数据树")
    public CommonResult<List<DictDataExtDO>> getDictDataTree() {
        return CommonResult.success(dictDataExtService.getDictDataTree());
    }

    @GetMapping("/children")
    @Operation(summary = "获取子节点列表")
    public CommonResult<List<DictDataExtDO>> getChildren(@RequestParam("parentId") Long parentId) {
        return CommonResult.success(dictDataExtService.getChildren(parentId));
    }

    @GetMapping("/parent")
    @Operation(summary = "获取父节点")
    public CommonResult<DictDataExtDO> getParent(@RequestParam("id") Long id) {
        return CommonResult.success(dictDataExtService.getParent(id));
    }

    @GetMapping("/level")
    @Operation(summary = "获取层级")
    public CommonResult<Integer> getLevel(@RequestParam("id") Long id) {
        return CommonResult.success(dictDataExtService.getLevel(id));
    }

    @GetMapping("/children-by-parent-id")
    @Operation(summary = "根据字典值递归查询所有子节点")
    @Parameter(name = "parentId", description = "字典的顶级id 质地类别 传1，其他类型找后端确定", required = true, example = "1024")
    public CommonResult<List<DictDataExtDO>> getChildrenByValue(@RequestParam("parentId") Long parentId) {
        return CommonResult.success(dictDataExtService.getChildrenByParentId(parentId));
    }


}